﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Xml;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.CompanyService;
using WebApp.DomainService;
using WebApp.ProductService;
using WebApp.TypeService;

namespace WebApp
{
    public partial class Product1 : System.Web.UI.Page
    {
        ProductServiceClient ProductClient;
        TypeServiceClient TypeClient;
        DomainServiceClient DomainClient;
        CompanyServiceClient CompanyClient;
        protected void Page_Load(object sender, EventArgs e)
        {
            ProductClient = new ProductServiceClient();
            TypeClient = new TypeServiceClient();
            DomainClient = new DomainServiceClient();
            CompanyClient = new CompanyServiceClient();

            LoadAllOptions();
            InitAllProducts();
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("App_Data/Product.xml"));
            ASPxDataView1.DataSourceID = null;
            ASPxDataView1.DataSource = ds;
            ASPxDataView1.DataBind();
            Page.MaintainScrollPositionOnPostBack = true;
        }
        //Dua du lieu vao bang

        private List<ProductDTO> GetAllProduct()
        {
            List<ProductDTO> newlist = new List<ProductDTO>();
            newlist.AddRange(ProductClient.GetAllProducts());
            return newlist;

        }

        protected void btndetail_Click(object sender, EventArgs e)
        {
            ProductDTO pro;
            ProductClient.SetID(Convert.ToInt32(Request.Params["ProductID"]));
            pro = ProductClient.GetProductByID();
            pro.ViewCount = pro.ViewCount + 1;
            ProductClient.SetDTO(pro);
           if (ProductClient.UpdateProduct()) ;
        }

        private void LoadAllOptions()
        {
            //load domain
            DropdownlistDomain.Items.Clear();
            List<DomainDTO> newList1 = GetAllDomain();
            foreach (DomainDTO item in newList1)
            {
                ListItem newlistitem = new ListItem(item.DomainName, item.DomainID.ToString());
                DropdownlistDomain.Items.Add(newlistitem);
            }

            //load type 
            DropdownlistType.Items.Clear();
            List<TypeDTO> newList = GetAllType();
            foreach (TypeDTO item in newList)
            {
                ListItem newlistitem = new ListItem(item.TypeName, item.TypeID.ToString());
                DropdownlistType.Items.Add(newlistitem);
            }

            //load company
            DropdownlistCompany.Items.Clear();
            List<CompanyDTO> newList2 = GetAllCompany();
            foreach (CompanyDTO item in newList2)
            {
                ListItem newlistitem = new ListItem(item.CompanyName, item.CompanyID.ToString());
                DropdownlistCompany.Items.Add(newlistitem);
            }
        }

        private List<TypeDTO> GetAllType()
        {
            List<TypeDTO> newtype = new List<TypeDTO>();
            newtype.AddRange(TypeClient.GetAllTypes());
            return newtype;
        }

        private List<DomainDTO> GetAllDomain()
        {
            List<DomainDTO> newdomain = new List<DomainDTO>();
            newdomain.AddRange(DomainClient.GetAllDomains());
            return newdomain;
        }

        private List<CompanyDTO> GetAllCompany()
        {
            List<CompanyDTO> newcompany = new List<CompanyDTO>();
            newcompany.AddRange(CompanyClient.GetAllCompanies());
            return newcompany;
        }

        private void SearchProduct()
        {
            string name = TextBoxName.Text;
            int domainid = -1;
            int typeid = -1;
            int companyid = -1;

            if (DropdownlistDomain.SelectedItem != null)
            {
                domainid = int.Parse(DropdownlistDomain.SelectedValue);
            }

            if (DropdownlistType.SelectedItem != null)
            {
                typeid = int.Parse(DropdownlistType.SelectedValue);
            }
            if (DropdownlistCompany.SelectedItem != null)
            {
                companyid = int.Parse(DropdownlistCompany.SelectedValue);
            }
            List<ProductDTO> newlist = new List<ProductDTO>();
            newlist.AddRange(ProductClient.GetProductByOption(name, typeid, domainid, companyid));


                WriteXMLToFile(newlist);
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("App_Data/Product.xml"));
                ASPxDataView1.DataSourceID = null;
                ASPxDataView1.DataSource = null;
                ASPxDataView1.DataBind();
            Response.Redirect("Product.aspx?TypeID=" + DropdownlistType.SelectedItem.Value);
       

        }
        private void InitAllProducts()
        {
            List<ProductDTO> newlist = GetAllProduct();
            WriteXMLToFile(newlist);
        }

        private void WriteXMLToFile(List<ProductDTO> List)
        {
            XmlTextWriter xmlwriter = new XmlTextWriter(Server.MapPath("App_Data/Product.xml"), Encoding.UTF8);
            xmlwriter.Formatting = Formatting.Indented;
            xmlwriter.WriteStartDocument();
            xmlwriter.WriteComment("Ghi dữ liệu ra XML");
            xmlwriter.WriteStartElement("Product");

            //Duyet tat ca san pham
            
            foreach (ProductDTO item in List)
            {
                xmlwriter.WriteStartElement("Product");
                xmlwriter.WriteAttributeString("ProductName", item.ProductName);
                xmlwriter.WriteAttributeString("ProductID", item.ProductID.ToString());
                xmlwriter.WriteElementString("Image", item.Image);
                xmlwriter.WriteElementString("Price", item.Price.ToString());
                xmlwriter.WriteElementString("ProPrice", item.ProPrice.ToString());
                xmlwriter.WriteElementString("View",item.ViewCount.ToString());
                xmlwriter.WriteEndElement();
            }

            xmlwriter.WriteEndElement();
            xmlwriter.WriteEndDocument();
            xmlwriter.Flush();
            xmlwriter.Close();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SearchProduct();
        }
    }       
}
